package com.sip.stream.activity.preview.model;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;

import com.sip.stream.ClientApp;
import com.sip.stream.R;
import com.sip.stream.databinding.IncludeStatusLayoutBinding;
import com.sip.stream.rxbus.RxBus;
import com.sip.stream.utils.ZFYStatusType;
import com.sip.stream.utils.zfy.adapter.ZFYStatusAdapter;
import com.sip.stream.utils.zfy.proxy.PlatformProxy;
import com.sip.stream.utils.zfy.status.ZFYStatus;

import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class ZFYStatusViewModel {
    private static final String TAG = "ZFYStatusViewModel";
    private ZFYStatusAdapter adapter;
    private IncludeStatusLayoutBinding binding;
    private boolean isForground = true;

    public static int getResIdBySignalStrength(int i) {
        return i != 1 ? i != 2 ? i != 3 ? i != 4 ? R.drawable.status_mobile_0 : R.drawable.status_mobile_4 : R.drawable.status_mobile_3 : R.drawable.status_mobile_2 : R.drawable.status_mobile_1;
    }

    public static int getResIdByWIFIStrength(int i) {
        return i != 1 ? i != 2 ? i != 3 ? i != 4 ? R.drawable.status_wifi_0 : R.drawable.status_wifi_4 : R.drawable.status_wifi_3 : R.drawable.status_wifi_2 : R.drawable.status_wifi_1;
    }

    public ZFYStatusViewModel(IncludeStatusLayoutBinding includeStatusLayoutBinding, Context context) {
        this.binding = includeStatusLayoutBinding;
    }

    public void setUpStatus() {
        try {
            ZFYStatus.getInstance().setAirplaneState(Settings.Global.getInt(ClientApp.clientApp.getApplicationContext().getContentResolver(), "airplane_mode_on"));
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void refreshBinding() {
        if (this.binding == null) {
            return;
        }
        int batteryLevel = ZFYStatus.getInstance().getBatteryLevel();
        this.binding.tvBattery.setText(batteryLevel + "%");
        if (ZFYStatus.getInstance().isPowerconnected()) {
            this.binding.ivBattery.setImageDrawable(getDrawable(R.drawable.ic_status_battery_add));
        } else if (batteryLevel > 75) {
            this.binding.ivBattery.setImageDrawable(getDrawable(R.drawable.ic_status_battery_76_100));
        } else if (batteryLevel > 50) {
            this.binding.ivBattery.setImageDrawable(getDrawable(R.drawable.ic_status_battery_51_75));
        } else if (batteryLevel > 25) {
            this.binding.ivBattery.setImageDrawable(getDrawable(R.drawable.ic_status_battery_26_50));
        } else if (batteryLevel > 10) {
            this.binding.ivBattery.setImageDrawable(getDrawable(R.drawable.ic_status_battery_11_25));
        } else {
            this.binding.ivBattery.setImageDrawable(getDrawable(R.drawable.ic_status_battery_0_10));
        }
        this.binding.ivLock.setVisibility(ZFYStatus.getInstance().isLocked() ? View.VISIBLE : View.GONE);
        this.binding.ivAirplane.setVisibility(ZFYStatus.getInstance().getAirplaneState() > 0 ? View.VISIBLE : View.GONE);
        if (ZFYStatus.getInstance().getWifiSignalLevel() >= 0) {
            this.binding.layoutWifi.setVisibility(View.VISIBLE);
            this.binding.ivWifi.setImageDrawable(getDrawable(getResIdByWIFIStrength(ZFYStatus.getInstance().getWifiSignalLevel())));
            int wifiVersion = ZFYStatus.getInstance().getWifiVersion();
            if (ZFYStatus.getInstance().getWifiVersion() > 4) {
                this.binding.tvWifi.setVisibility(View.VISIBLE);
                this.binding.tvWifi.setText(wifiVersion);
            } else {
                this.binding.tvWifi.setVisibility(View.GONE);
            }
        } else {
            this.binding.layoutWifi.setVisibility(View.GONE);
        }
        if (ZFYStatus.getInstance().getMobileNetworkType() >= 0) {
            this.binding.layoutMobile.setVisibility(View.VISIBLE);
            this.binding.ivMobile.setImageDrawable(getDrawable(getResIdBySignalStrength(ZFYStatus.getInstance().getMobileSignalStrength())));
            this.binding.tvMobile.setText(ZFYStatus.getInstance().getMobileNetworkStr());
        } else {
            this.binding.layoutMobile.setVisibility(View.GONE);
        }

        if (ZFYStatus.getInstance().isUsbconnect() || ZFYStatus.getInstance().isUVCCameraIn()) {
            this.binding.ivUsb.setVisibility(View.VISIBLE);
        } else {
            this.binding.ivUsb.setVisibility(View.GONE);
        }
        if (ZFYStatus.getInstance().isGpsReady())
        {
            Drawable drawable = getDrawable(ZFYStatus.getInstance().isBdOnly() ? R.drawable.ic_vector_gps_b : R.drawable.ic_vector_gps_n);
            if (System.currentTimeMillis() - ZFYStatus.getInstance().getLocation().getLocationTime() > 60000) {
                drawable.setTint(-7829368);
            } else {
                drawable.setTint(getColor(R.color.green_zfy));
            }
            this.binding.ivGps.setVisibility(View.VISIBLE);
            this.binding.ivGps.setImageDrawable(drawable);
        } else {
            this.binding.ivGps.setVisibility(View.GONE);
        }
        this.binding.ivBle.setVisibility(ZFYStatus.getInstance().isOpenBle() ? View.VISIBLE : View.GONE);
    }

    public Drawable getDrawable(int i) {
        return ClientApp.clientApp.getResources().getDrawable(i);
    }

    public int getColor(int i) {
        return ClientApp.clientApp.getResources().getColor(i);
    }

    public synchronized void refresh()
    {
        if (this.isForground)
        {
            if (this.binding != null) {
                Single.just(Boolean.TRUE).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver<Boolean>() {
                    @Override
                    public void onError(Throwable th) {
                    }

                    @Override
                    public void onSubscribe(Disposable disposable) {
                    }

                    @Override
                    public void onSuccess(Boolean bool) {
                        ZFYStatusViewModel.this.refreshBinding();
                    }
                });
                return;
            }
            this.adapter.getDatasource().clear();

            if (ZFYStatus.getInstance().getBatteryLevel() < 0) {
                this.adapter.notifyDataSetChanged();
                return;
            }
            if (ZFYStatus.getInstance().isLocked()) {
                this.adapter.getDatasource().add(ZFYStatusType.LOCK);
            }
            this.adapter.getDatasource().add(ZFYStatusType.BATTERY);
            if (ZFYStatus.getInstance().getAirplaneState() > 0) {
                this.adapter.getDatasource().add(ZFYStatusType.AIRPLANE);
            } else {
                if (ZFYStatus.getInstance().getWifiSignalLevel() >= 0) {
                    this.adapter.getDatasource().add(ZFYStatusType.WIFI);
                }
                if (ZFYStatus.getInstance().getMobileNetworkType() >= 0) {
                    this.adapter.getDatasource().add(ZFYStatusType.MOBILENETWORKT);
                }
            }

            if (ZFYStatus.getInstance().isUsbconnect() || ZFYStatus.getInstance().isUVCCameraIn()) {
                this.adapter.getDatasource().add(ZFYStatusType.USB);
            }
            this.adapter.getDatasource().add(ZFYStatusType.GPS);
            if (ZFYStatus.getInstance().isOpenBle()) {
                this.adapter.getDatasource().add(ZFYStatusType.BLUETOOTH);
            }
            Single.just(Boolean.TRUE).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver<Boolean>() {
                @Override
                public void onError(Throwable th) {
                }

                @Override
                public void onSubscribe(Disposable disposable) {
                }

                @Override
                public void onSuccess(Boolean bool) {
                    ZFYStatusViewModel.this.adapter.notifyDataSetChanged();
                }
            });
        }
    }

    public void onResume() {
        this.isForground = true;
        refresh();
        this.binding.layoutRoot.setVisibility(View.VISIBLE);
        RxBus.getDefault().subscribe(this, "ZFYStatusEvent", new RxBus.Callback<String>() {
            @Override
            public void onEvent(String str) {
                ZFYStatusViewModel.this.refresh();
                PlatformProxy.getInstance().updateStatus();
            }
        });
    }

    public void onPause() {
        this.isForground = false;
        ZFYStatusAdapter zFYStatusAdapter = this.adapter;
        if (zFYStatusAdapter != null) {
            zFYStatusAdapter.getDatasource().clear();
        }
        this.binding.layoutRoot.setVisibility(View.INVISIBLE);
        RxBus.getDefault().unregister(this);
    }
}
